package fr.istic.linkedcv.server.servicesImpl;

import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import fr.istic.linkedcv.client.CVCli;
import fr.istic.linkedcv.client.services.CVService;
import fr.istic.linkedcv.server.CVImpl;
import fr.istic.linkedcv.server.PMF;
import fr.istic.linkedcv.server.PersonneImpl;

public class CVServiceImpl extends RemoteServiceServlet implements CVService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9218316358560592475L;

	@Override
	public boolean createCV(Map<String, String> cv) {
		
		CVImpl cvImpl = new CVImpl();
		
			
			cvImpl.setObjectif(cv.get("objectif"));
			cvImpl.setEducation(cv.get("divers"));
			cvImpl.setExperience_pro(cv.get("experience_pro"));
			cvImpl.setCompetences(cv.get("competences"));
			
			cvImpl.setDivers(cv.get("divers"));
			
		
			assocCVToPersonne(cvImpl);
		return true;
	}
	
	private void assocCVToPersonne(CVImpl cvImpl){
		PersistenceManagerFactory pmf = PMF.get();
		PersistenceManager pm = pmf.getPersistenceManager();
		HttpSession session = this.getThreadLocalRequest().getSession();
		try{
			PersonneImpl personneImpl = pm.getObjectById(PersonneImpl.class, (String) session.getAttribute("login"));
			
			personneImpl.setCvImpl(cvImpl);
			pm.makePersistent(personneImpl);
		} finally {
			pm.close();
		}
	}

	@Override
	public CVCli getCV(String login) {
		CVCli res = null;
		PersistenceManagerFactory pmf = PMF.get();
		PersistenceManager pm = pmf.getPersistenceManager();
		HttpSession session = this.getThreadLocalRequest().getSession();
		try {
			PersonneImpl personneImpl = pm.getObjectById(PersonneImpl.class, login);
			CVImpl cv = personneImpl.getCvImpl();
			res = (cv!=null)?createCVCli(cv):null;
		}finally {
			pm.close();
		}
		return res;
	}

	private PersonneImpl getPerson(String login, PersistenceManager pm) {
		PersonneImpl res = null;
		
		Query q = pm.newQuery(PersonneImpl.class);
		q.setFilter("login == \"" + login+"\"");
		try {

			List<PersonneImpl> listPersonne = (List<PersonneImpl>) q.execute();
			if (!listPersonne.isEmpty()) {				
				res = listPersonne.iterator().next();				
			}
		} finally {
			q.closeAll();
		}
		return res;
	}
	
	public CVCli createCVCli(CVImpl cvImpl) {
		CVCli cvCli = new CVCli();
		cvCli.setObjectif(cvImpl.getObjectif());
		cvCli.setEducation(cvImpl.getEducation());
		cvCli.setExperience_pro(cvImpl.getExperience_pro());
		cvCli.setCompetences(cvImpl.getCompetences());
		cvCli.setDivers(cvImpl.getDivers());
		return cvCli;

	}

	@Override
	public boolean updateCV(Map<String, String> cv) {
		
			PersistenceManagerFactory pmf = PMF.get();
			PersistenceManager pm = pmf.getPersistenceManager();
			HttpSession session = this.getThreadLocalRequest().getSession();
			try{
				
				PersonneImpl personneImpl = pm.getObjectById(PersonneImpl.class, (String) session.getAttribute("login"));
				CVImpl cvImpl = personneImpl.getCvImpl();
				cvImpl.setObjectif(cv.get("objectif"));
				cvImpl.setEducation(cv.get("divers"));
				cvImpl.setExperience_pro(cv.get("experience_pro"));
				cvImpl.setCompetences(cv.get("competences"));
				
				pm.makePersistent(personneImpl);
				
			} finally {
				pm.close();
			}
		
		return true;
	}

}
